package org.dromara.resource.controller.api;

import cn.hutool.core.convert.Convert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.oss.core.OssClient;
import org.dromara.common.oss.entity.UploadResult;
import org.dromara.common.oss.factory.OssFactory;
import org.dromara.resource.api.domain.RemoteFile;
import org.dromara.resource.domain.SysOssExt;
import org.dromara.resource.domain.bo.SysOssBo;
import org.dromara.resource.domain.vo.SysOssVo;
import org.dromara.resource.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: zhou shuai
 * @date: 2024/9/26 20:31
 * @version: v1
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/resource/file")
public class ResourceFileApiController {

    private final ISysOssService sysOssService;

    /**
     * 文件上传请求
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<RemoteFile> fileUpload(@RequestPart("avatarfile") MultipartFile avatarfile) {
        try {
            String originalFilename = avatarfile.getOriginalFilename();
            String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
            OssClient storage = OssFactory.instance();
            UploadResult uploadResult = storage.uploadSuffix(avatarfile.getBytes(), suffix, avatarfile.getContentType());
            // 保存文件信息
            SysOssBo oss = new SysOssBo();
            oss.setUrl(uploadResult.getUrl());
            oss.setFileSuffix(suffix);
            oss.setFileName(uploadResult.getFilename());
            oss.setOriginalName(originalFilename);
            oss.setService(storage.getConfigKey());
            SysOssExt ext1 = new SysOssExt();
            ext1.setFileSize((long) avatarfile.getBytes().length);
            String extStr = JsonUtils.toJsonString(ext1);
            oss.setExt1(extStr);
            sysOssService.insertByBo(oss);
            RemoteFile sysFile = new RemoteFile();
            sysFile.setOssId(oss.getOssId());
            sysFile.setName(uploadResult.getFilename());
            sysFile.setUrl(uploadResult.getUrl());
            sysFile.setOriginalName(originalFilename);
            sysFile.setFileSuffix(suffix);
            sysFile.setExt1(extStr);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            throw new ServiceException("上传文件失败");
        }
    }

    /**
     * 通过ossId查询对应的url
     *
     * @param ossIds ossId串逗号分隔
     * @return url串逗号分隔
     */
    @GetMapping("/url/byOssIds")
    public R<String> getFileUrlByOssIds(@RequestParam("ossIds") String ossIds) {
        return R.ok(sysOssService.selectUrlByIds(ossIds));
    }

    /**
     * 通过ossId查询列表
     *
     * @param ossIds ossId串逗号分隔
     * @return 列表
     */
    @GetMapping("/list/byOssIds")
    public R<List<RemoteFile>> getFileListByOssIds(@RequestParam("ossIds") String ossIds) {
        List<SysOssVo> sysOssVos = sysOssService.listByIds(StringUtils.splitTo(ossIds, Convert::toLong));
        return R.ok(MapstructUtils.convert(sysOssVos, RemoteFile.class));
    }

}
